#!/bin/bash
echo "Monitor Start"

#所需监控的命令配置文件路径
file=/home/tool/golearn/src/emicro_5/grpc_util/monitor_process/process.conf
#当前时间
currTime=`date +"%Y-%m-%d %H:%M:%S"`
#docker容器的前缀，用来区分docker进程还是linux进程
docker_str="docker:"

#监控
while read command
do
  is_docker=0
  if [[ ${command} == ${docker_str}* ]]; then
      is_docker=1
  fi
  #判断为docker容器
  if [ ${is_docker} -eq 1 ]; then
      exists=`docker inspect --format '{{.State.Running}}' "${command:7}"`
      if [ $exists != "true" ]; then
        echo "${currTime}: docker-${command:7} is restart" >> /home/tool/golearn/src/emicro_5/grpc_util/monitor_process/monitor_err.log
        `docker start "${command:7}"`
      fi
  #判断为grpc服务等
  else
      count=`ps aux|grep -c "${command}"|grep -v "grep"`
      if [ $count -lt 2 ]; then
        echo "${currTime}: ${command} is restart" >> /home/tool/golearn/src/emicro_5/grpc_util/monitor_process/monitor_err.log
        `nohup ${command} > /dev/null 2>&1 &`
      fi
  fi
done < ${file}
